//v.2.1 build 90226

/*
Copyright DHTMLX LTD. http://www.dhtmlx.com
To use this component please contact sales@dhtmlx.com to obtain license
*/
/**
*	@desc: integration with dhtmlxCombo editor
*	@returns: dhtmlxGrid cell editor object
*/

dhtmlXGridObject.prototype._init_point_bcg=dhtmlXGridObject.prototype._init_point;
dhtmlXGridObject.prototype._init_point = function(){
	
	if(!window.dhx_globalImgPath) window.dhx_globalImgPath = this.imgURL;

	this._col_combos=[];
	for (var i=0; i<this._cCount; i++){
		if(this.cellType[i]=="combo")
			this._col_combos[i] = eXcell_combo.prototype.initCombo.call({grid:this});
	}
	if(!this._loading_handler_set){
		this._loading_handler_set = this.attachEvent("onXLE",function(a,b,c,xml){
			
			eXcell_combo.prototype.fillColumnCombos(this,xml);	
			this.detachEvent(this._loading_handler_set);
		})
	}
	this._init_point=this._init_point_bcg;
	if (this._init_point) this._init_point();
};


function eXcell_combo(cell){
	
	if(!cell) return;

	this.cell = cell; 
	
	this.grid = cell.parentNode.grid;

	
				
		/**
		*	@desc: method called by grid to start editing
		*/
	this.edit = function(){
		if(!window.dhx_globalImgPath) window.dhx_globalImgPath = this.grid.imgURL;
		this.val=this.getValue();
		var val = this.getText();  
		if(this.cell._clearCell) val="";
		this.cell.innerHTML = "";

		if(!this.cell._brval)
			this.combo = (this.grid._realfake?this.grid._fake:this.grid)._col_combos[this.cell._cellIndex];
		else
			this.combo = this.cell._brval;
			
		this.cell.appendChild(this.combo.DOMParent);
		this.combo.DOMParent.style.margin="0";
		if(this.grid.customListColor) { this.combo.DOMlist.style.backgroundColor = this.grid.customListColor;}
		this.combo.DOMelem_input.focus();
		this.combo.setSize(this.cell.offsetWidth-2);
		this.combo.ncm=true;
		if(!this.combo._xml){ 

			
			if(this.combo.getIndexByValue(this.cell.combo_value)!=-1)
				this.combo.selectOption(this.combo.getIndexByValue(this.cell.combo_value));
			else {
				if(this.combo.getOptionByLabel(val))
					this.combo.selectOption(this.combo.getIndexByValue(this.combo.getOptionByLabel(val).value));
				else this.combo.setComboText(val);
			}

		}
		else this.combo.setComboText(val);
		
		this.combo.ncm=false;
		this.combo.openSelect();
		
		
	}
	
	this.selectComboOption = function(val,obj){
		obj.selectOption(obj.getIndexByValue(obj.getOptionByLabel(val).value));
	}
	
		/**
		*	@desc: get real value of the cell
		*/
		
	this.getValue = function(val){
		
		return this.cell.combo_value;
	}

	this.getText = function(val){
		return this.getTitle();
	}

	/**
	*	@desc: set formated value to the cell
	*/
	this.setValue = function(val){
		
		for(var i = 0; i < this.cell.parentNode.childNodes.length; i++){
			this.cell.parentNode.childNodes[i].tabIndex = 0;
		}
			
			
		if(typeof(val)=="object"){
			this.cell._brval = this.initCombo();
			var index = this.cell._cellIndex;
			var idd = this.cell.parentNode.idd;
			this.cell.combo_value = val.firstChild.data;
			this.setComboOptions(this.cell._brval,val,this.grid,index,idd);
				
		}else{
			this.cell.combo_value = val;	
			var cm=null;
			if (cm = this.grid._col_combos[this.cell._cellIndex])
				val=(cm.getOption(val)||{}).text||val;
						
			if ((val||"").toString()._dhx_trim()=="")
				val = null;
            
			if (val!==null)
 			    this.setCValue(val);
            else{
                 this.setCValue("&nbsp;",val);
				 this.cell._clearCell=true;	 
			}	
		}
	}
	            
		/**
		*	@desc: this method called by grid to close editor
		*   @type: private 
		*/
	this.detach = function(){
	
		this.cell.removeChild(this.combo.DOMParent);
		var val = this.cell.combo_value;
		if (!this.combo.getComboText() || this.combo.getComboText().toString()._dhx_trim()==""){
			this.setCValue("&nbsp;");
			this.cell._clearCell=true;
    	}
		else{
			this.setCTxtValue(this.combo.getComboText(),this.combo.getActualValue());
			this.cell._clearCell = false;
		}
		this.combo._confirmSelection();
		this.cell.combo_value = this.combo.getActualValue();
		this.combo.closeAll();
		this.grid._still_active=true;
		this.grid.setActive(1);
		return val!=this.cell.combo_value;
	}
}

				
eXcell_combo.prototype = new eXcell;

		/**
		*	@desc: create combo object
		*   @returns: combo object
		*   @type: private 		  
		*/
eXcell_combo.prototype.initCombo = function(){
	
	container = document.createElement("DIV");
	
	combo = new dhtmlXCombo(container,"combo",0);
	
	combo.DOMelem.className+=" fake_editable";
	grid = this.grid;
	combo.DOMelem.onselectstart=function(){event.cancelBubble=true; return true;};
	combo.attachEvent("onKeyPressed",function(ev){if(ev==13) {grid.editStop();if(grid._fake) grid._fake.editStop()}})
	combo.attachEvent("onBlur",function(){if(!this.ncm){grid.editStop();if(grid._fake) grid._fake.editStop()}})
	combo.DOMelem.style.border = "0px";
	combo.DOMelem.style.height = "18px";
	return combo;

}
		
eXcell_combo.prototype.fillColumnCombos = function(grid,xml){
	if (!xml) return;
	grid.combo_columns = grid.combo_columns||[];
	columns = grid.xmlLoader.doXPath("//column",xml);
	for(var i = 0; i < columns.length; i++){
		if(columns[i].getAttribute("type")=="combo"){
			grid.combo_columns[grid.combo_columns.length] = i; 
			
			this.setComboOptions(grid._col_combos[i],columns[i],grid,i);
			
		}
	}
	
}
		/**
		*	@desc: this method sets combo options and prorerties 
		*   @type: private 
		*/

eXcell_combo.prototype.setComboOptions = function(combo,obj,grid,index,idd){
	if(convertStringToBoolean(obj.getAttribute("xmlcontent"))){

		if(!obj.getAttribute("source")){
			options = obj.childNodes;
			for (var i=0; i < options.length; i++){
				if(options[i].tagName =="option"){
					var text_opt = options[i].firstChild? options[i].firstChild.data:"";
					combo.addOption(options[i].getAttribute("value"),text_opt);
				}
			}
			if(arguments.length == 4){
					grid.forEachRow(function(id){
						var c = grid.cells(id,index);
						if(!c.cell._brval&&!c.cell._cellType){
							if(c.cell.combo_value=="") c.setCValue("&nbsp;");
							else{
								if(!combo.getOption(c.cell.combo_value))
									c.setCValue(c.cell.combo_value);
								else c.setCValue(combo.getOption(c.cell.combo_value).text);
							}
						}
					});	
				}
				else {
					var c = (this.cell)?this:grid.cells(idd,index);
					if(obj.getAttribute("text")) {
						if(obj.getAttribute("text")._dhx_trim()=="") c.setCValue("&nbsp;"); 
						else c.setCValue(obj.getAttribute("text")); 
					}
					else{
						if((!c.cell.combo_value)||(c.cell.combo_value._dhx_trim()=="")) c.setCValue("&nbsp;");
						else{
							if(!combo.getOption(c.cell.combo_value))
								c.setCValue(c.cell.combo_value);
							else c.setCValue(combo.getOption(c.cell.combo_value).text);
						}
					}
			}
			
		}				
	}

	if(obj.getAttribute("source")){
		if(obj.getAttribute("auto")&&convertStringToBoolean(obj.getAttribute("auto"))){
		
			if(obj.getAttribute("xmlcontent")){
				var c = (this.cell)?this:grid.cells(idd,index);
				if(obj.getAttribute("text")) c.setCValue(obj.getAttribute("text"));
			}
			else{
				grid.forEachRow(function(id){
					var c = grid.cells(id,index);
					if(!c.cell._brval&&!c.cell._cellType){
						var str = c.cell.combo_value.toString();
						if(str.indexOf("^")!=-1){
							var arr = str.split("^");
							c.cell.combo_value = arr[0];
							c.setCValue(arr[1]);
						}
					}
				});	
			}
			combo.enableFilteringMode(true,obj.getAttribute("source"),convertStringToBoolean(obj.getAttribute("cache")||true),convertStringToBoolean(obj.getAttribute("sub")||false));	
		
		}
		else {
			var that = this;
			var length = arguments.length; 
			combo.loadXML(obj.getAttribute("source"),function(){
				if(length == 4){
					grid.forEachRow(function(id){
						var c = grid.cells(id,index);
						if(!c.cell._brval&&!c.cell._cellType){
							if(combo.getOption(c.cell.combo_value))
								c.setCValue(combo.getOption(c.cell.combo_value).text);
							else 
								c.setCValue(c.cell.combo_value);
						}
					});	
				}
				else {
					//var c = (that.cell)? that : grid.cells(idd,index);
					var c = grid.cells(idd,index);
					//c.setCValue(obj.getAttribute("text")); 
					if(combo.getOption(c.cell.combo_value))
						c.setCValue(combo.getOption(c.cell.combo_value).text);
					else 
						c.setCValue(c.cell.combo_value);
				}
			});
			
		}
	}
	if(!obj.getAttribute("auto")||!convertStringToBoolean(obj.getAttribute("auto"))){
	
		if(obj.getAttribute("editable")&&!convertStringToBoolean(obj.getAttribute("editable"))) combo.readonly(true);
		if(obj.getAttribute("filter")&&convertStringToBoolean(obj.getAttribute("filter"))) combo.enableFilteringMode(true);	
		
	}

}

		/**
		*	@desc: gets dhtmlxCombo object for specified cell  
		*   @returns: combo object
		*   @type: public 
		*/
eXcell_combo.prototype.getCellCombo = function(){
	
	if(this.cell._brval) return this.cell._brval;
	else {
		this.cell._brval = this.initCombo();
		return this.cell._brval;
	}
}
		/**
		*	@desc: gets dhtmlxCombo object for specified column  
		*   @param: index - column index
		*   @returns: combo object 
		*   @type: public
		*/

dhtmlXGridObject.prototype.getColumnCombo = function(index){
	if(this._col_combos&&this._col_combos[index]) return this._col_combos[index];
	else{
		if(!this._col_combos) this._col_combos=[];
		this._col_combos[index] = eXcell_combo.prototype.initCombo.call({grid:this});
		return this._col_combos[index];
	}
}


//(c)dhtmlx ltd. www.dhtmlx.com
